package com.ruoyi.system.service.impl.cryptocard;

import java.util.List;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.system.domain.reseller.VsDramasReseller;
import com.ruoyi.system.domain.usable.VsDramasUsable;
import com.ruoyi.system.domain.vip.VsDramasVip;
import com.ruoyi.system.mapper.reseller.VsDramasResellerMapper;
import com.ruoyi.system.mapper.usable.VsDramasUsableMapper;
import com.ruoyi.system.mapper.user.VsUserMapper;
import com.ruoyi.system.mapper.vip.VsDramasVipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.cryptocard.VsDramasCryptocardMapper;
import com.ruoyi.system.domain.cryptocard.VsDramasCryptocard;
import com.ruoyi.system.service.cryptocard.IVsDramasCryptocardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 卡密Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-27
 */
@Service
public class VsDramasCryptocardServiceImpl implements IVsDramasCryptocardService 
{
    @Autowired
    private VsDramasCryptocardMapper vsDramasCryptocardMapper;
    @Autowired
    private VsDramasVipMapper vipMapper;
    @Autowired
    private VsDramasResellerMapper resellerMapper;
    @Autowired
    private VsDramasUsableMapper usableMapper;

    @Autowired
    private VsUserMapper vsUserMapper;

    /**
     * 查询卡密
     * 
     * @param id 卡密主键
     * @return 卡密
     */
    @Override
    public VsDramasCryptocard selectVsDramasCryptocardById(Integer id)
    {
        return vsDramasCryptocardMapper.selectVsDramasCryptocardById(id);
    }

    /**
     * 查询卡密列表
     * 
     * @param vsDramasCryptocard 卡密
     * @return 卡密
     */
    @Override
    public List<VsDramasCryptocard> selectVsDramasCryptocardList(VsDramasCryptocard vsDramasCryptocard)
    {
        List<VsDramasCryptocard> list = vsDramasCryptocardMapper.selectVsDramasCryptocardList(vsDramasCryptocard);
        for (VsDramasCryptocard v : list) {
            if (v.getUserId() != null) {
                VsUser user = vsUserMapper.selectVsUserById(v.getUserId());
                v.setVsUser(user);
            }
            if("vip".equals(v.getType())) {
                VsDramasVip vsDramasVip = vipMapper.selectVsDramasVipById(Math.toIntExact(v.getItemId()));
                v.setItemName(vsDramasVip.getTitle());
            } else if("reseller".equals(v.getType())) {
                VsDramasReseller vsDramasReseller = resellerMapper.selectVsDramasResellerById(Math.toIntExact(v.getItemId()));
                v.setItemName(vsDramasReseller.getName());
            } else if("usable".equals(v.getType())) {
                VsDramasUsable vsDramasUsable = usableMapper.selectVsDramasUsableById(Math.toIntExact(v.getItemId()));
                v.setItemName(vsDramasUsable.getTitle());
            }
        }
        return list;
    }

    /**
     * 新增卡密
     * 
     * @param vsDramasCryptocard 卡密
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVsDramasCryptocard(VsDramasCryptocard vsDramasCryptocard)
    {
        vsDramasCryptocard.setSiteId(0L);
        vsDramasCryptocard.setCreatetime(System.currentTimeMillis());
        if (vsDramasCryptocard.getIssueNum() != null) {
            if (vsDramasCryptocard.getIssueNum() > 0) {
                Integer num = 0;
                for (int i = 0; i < vsDramasCryptocard.getIssueNum(); i++) {
                    checkPwdType(vsDramasCryptocard);
                    int cryptocard = vsDramasCryptocardMapper.insertVsDramasCryptocard(vsDramasCryptocard);
                    num += cryptocard;
                }
                if (num == vsDramasCryptocard.getIssueNum()) {
                    return vsDramasCryptocard.getIssueNum();
                } else {
                    return 0;
                }
            }
            return 0;
        } else {
            checkPwdType(vsDramasCryptocard);
            return vsDramasCryptocardMapper.insertVsDramasCryptocard(vsDramasCryptocard);
        }
    }

    private void checkPwdType(VsDramasCryptocard vsDramasCryptocard) {
        if ("digitLetter".equals(vsDramasCryptocard.getPwdType())) {
            String pwd = RandomUtils.generateRandomMixedString(vsDramasCryptocard.getKeyLength());
            vsDramasCryptocard.setPwd(pwd);
        } else if ("letter".equals(vsDramasCryptocard.getPwdType())) {
            String pwd = RandomUtils.generateRandomLetters(vsDramasCryptocard.getKeyLength());
            vsDramasCryptocard.setPwd(pwd);
        } else if ("digit".equals(vsDramasCryptocard.getPwdType())) {
            String pwd = RandomUtils.generateRandomDigits(vsDramasCryptocard.getKeyLength());
            vsDramasCryptocard.setPwd(pwd);
        } else if ("noZero".equals(vsDramasCryptocard.getPwdType())) {
            String pwd = RandomUtils.generateRandomNoZeroString(vsDramasCryptocard.getKeyLength());
            vsDramasCryptocard.setPwd(pwd);
        }
    }

    /**
     * 修改卡密
     * 
     * @param vsDramasCryptocard 卡密
     * @return 结果
     */
    @Override
    public int updateVsDramasCryptocard(VsDramasCryptocard vsDramasCryptocard)
    {
        return vsDramasCryptocardMapper.updateVsDramasCryptocard(vsDramasCryptocard);
    }

    /**
     * 批量删除卡密
     * 
     * @param ids 需要删除的卡密主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasCryptocardByIds(Integer[] ids)
    {
        return vsDramasCryptocardMapper.deleteVsDramasCryptocardByIds(ids);
    }

    /**
     * 删除卡密信息
     * 
     * @param id 卡密主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasCryptocardById(Integer id)
    {
        return vsDramasCryptocardMapper.deleteVsDramasCryptocardById(id);
    }

    @Override
    public List<VsDramasCryptocard> selectVsDramasCryptocardInfo(VsDramasCryptocard vsDramasCryptocard) {
        return vsDramasCryptocardMapper.selectVsDramasCryptocardInfo(vsDramasCryptocard);
    }

    @Transactional
    @Override
    public int exchange(VsDramasCryptocard vsDramasCryptocard) {
        return vsDramasCryptocardMapper.exchange(vsDramasCryptocard);
    }
}
